package com.chwn.curiousdiary.download;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;

import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.provider.DownloadStoryStore;
import com.chwn.curiousdiary.utils.StorageUtils;

import java.io.File;


/**
 * Created by Myshikin on 2017/4/27.
 */

public class DownHelper {

    public static void downStory(Context context, StoryBean storyBean) {
        DownloadBean downloadBean = new DownloadBean();
        //        downloadBean.setSaveDirPath(getDownSave(context));
        downloadBean.setSaveDirPath(getDownSave(context, true));
        downloadBean.setDownloadStatus(DownloadStatus.DOWNLOAD_STATUS_INIT);
        downloadBean.setCompletedSize(0L);
        downloadBean.setTotalSize(0L);
        downloadBean.setUrl(storyBean.getStoryUrl());
        downloadBean.setStoryName(storyBean.getStoryTitle());
        downloadBean.setStoryDesc(storyBean.getStoryDetailTitle());
        downloadBean.setStroyShareURL(storyBean.getStroyShareURL());
        downloadBean.setStoryImage(storyBean.getStoryImage());
        downloadBean.setStoryId(storyBean.getStoryId());
        downloadBean.setStoryCreateTime(storyBean.getStoryCreateTime());
        downloadBean.setStoryListenCount(storyBean.getStoryListenCount());
        downloadBean.setTypeId(TextUtils.isEmpty(storyBean.getTypeId()) ? "-1" : storyBean.getTypeId());

        Intent i = new Intent(DownService.ADD_DOWNTASK);
        i.setAction(DownService.ADD_DOWNTASK);
        i.putExtra(Constant.DOWN_STORY, downloadBean);
        i.setPackage(Constant.PACKAGE_NAME);
        context.startService(i);
    }

    public static String getDownSave(Context context, boolean preferExternal) {
        File dataDirectory = StorageUtils.getDataDirectory(context, preferExternal);
        return dataDirectory.getAbsolutePath() + "/";
    }

    private static String getDownSave(Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/curiousDiary/");
            if (!file.exists()) {
                boolean r = file.mkdirs();
                if (!r) {
                    Toast.makeText(context, "储存卡无法创建文件", Toast.LENGTH_SHORT).show();
                    return null;
                }
                return file.getAbsolutePath() + "/";
            }
            return file.getAbsolutePath() + "/";
        } else {
            File file = new File(context.getFilesDir().getAbsolutePath() + "/curiousDiary/");
            if (!file.exists()) {
                boolean r = file.mkdirs();
                if (!r) {
                    Toast.makeText(context, "储存卡无法创建文件", Toast.LENGTH_SHORT).show();
                    return null;
                }
                return file.getAbsolutePath() + "/";
            }
            Toast.makeText(context, "没有储存卡", Toast.LENGTH_SHORT).show();
            return file.getAbsolutePath() + "/";
        }
    }

    public static boolean isDown(Context context, String stordId) {
        DownloadStoryStore instance = DownloadStoryStore.getInstance(context);
        DownloadBean downLoadedList = instance.getDownLoadedList(stordId);
        if (downLoadedList == null) {
            return false;
        }
        return true;
    }
}

